package com.bj58.disconf.web.service.config;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.baidu.disconf.core.common.constants.DisConfigTypeEnum;
import com.bj58.disconf.web.common.form.RequestListBase.Page;
import com.bj58.disconf.web.common.utils.DBHelper;
import com.bj58.disconf.web.service.config.bo.Config;
import com.bj58.disconf.web.service.config.dao.ConfigDao;
import com.bj58.disconf.web.utils.Constants;
import com.bj58.ub.common.db.DaoPageResult;
import com.google.common.collect.Lists;
import com.google.inject.Singleton;

/**
 * @author lemon
 * @version 2014-6-16
 */
@Singleton
public class ConfigDaoImpl implements ConfigDao {

    private static final Logger logger = LoggerFactory.getLogger(ConfigDaoImpl.class);

    public Long insert(Config config) {
        try {
            Object id = DBHelper.getInstance().getDao().sql.insert(config);
            if (id != null) {
                return (Long) id;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0l;
    }

    public Config getByID(long id) {
        try {
            List<Config> ulist = DBHelper.getInstance().getDao().sql.getListByIDS(Config.class, new Object[] { id });
            return ulist != null ? ulist.get(0) : null;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     *
     */
    public Config getByParameter(Long appid, Long envid, String version, String key, DisConfigTypeEnum disConfigTypeEnum) {
        try {
            logger.info("key=" + key);
            StringBuffer sb = new StringBuffer(100);
            List<Object> objs = Lists.newArrayList();
            sb.append("select * from config where appid = ?");
            objs.add(appid);
            sb.append(" and envid=?");
            objs.add(envid);
            sb.append(" and version=?");
            objs.add(version);
            sb.append(" and type=?");
            objs.add(disConfigTypeEnum.getType());
            sb.append(" and status = ?");
            objs.add(Constants.STATUS_NORMAL);

            List<Config> Configlist = DBHelper.getInstance().getDao().sql.getListBySQL(Config.class, sb.toString(), objs.toArray());
            return Configlist != null ? Configlist.get(0) : null;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 
     */
    public List<Config> getConfByAppEnv(Long appid, Long envid) {
        try {
            StringBuffer sb = new StringBuffer(100);
            List<Object> objs = Lists.newArrayList();
            sb.append(" select * from config where appid = ?");
            objs.add(appid);
            if (envid != null && envid.longValue() != -1l) {
                sb.append(" and envid=? ");
                objs.add(envid);
            }
            sb.append(" and status = ? ");
            objs.add(Constants.STATUS_NORMAL);

            List<Config> Configlist = DBHelper.getInstance().getDao().sql.getListBySQL(Config.class, sb.toString(), objs.toArray());
            return Configlist;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     *
     */
    public DaoPageResult<Config> getConfigList(Long appid, Long envid, String version, Page page) {

        //        DaoPage daoPage = DaoUtils.daoPageAdapter(page);
        //        List<Match> matchs = new ArrayList<Match>();
        //
        //        matchs.add(new Match(Columns.APP_ID, appId));
        //
        //        matchs.add(new Match(Columns.ENV_ID, envId));
        //
        //        matchs.add(new Match(Columns.VERSION, version));
        //
        //        matchs.add(new Match(Columns.STATUS, Constants.STATUS_NORMAL));
        //
        //        return page2(matchs, daoPage);

        try {
            DaoPageResult<Config> pageresult = new DaoPageResult<Config>();
            StringBuffer sb = new StringBuffer(100);
            sb.append("  appid = ");
            sb.append(appid);
            sb.append(" and envid=");
            sb.append(envid);
            sb.append(" and version='");
            sb.append(version);
            sb.append("' and status =  ");
            sb.append(Constants.STATUS_NORMAL);
            List<Config> Configlist = DBHelper.getInstance().getDao().sql.getListByPage(Config.class, "", "", page.getPageNo() + 1, page.getPageSize(), "");
            pageresult.setResult(Configlist);
            int count = DBHelper.getInstance().getDao().sql.getCount(Config.class, sb.toString());
            pageresult.setTotalCount(count);
            return pageresult;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }

    /**
     *
     */
    public List<Config> getConfigList(Long appid, Long envid, String version, Boolean hasValue) {

        //        List<Match> matchs = new ArrayList<Match>();
        //        matchs.add(new Match(Columns.APP_ID, appId));
        //        matchs.add(new Match(Columns.ENV_ID, envId));
        //        matchs.add(new Match(Columns.VERSION, version));
        //        matchs.add(new Match(Columns.STATUS, Constants.STATUS_NORMAL));
        //        if (hasValue) {
        //            return find(matchs, new ArrayList<Order>());
        //        } else {
        //            return findColumns(matchs, new String[] { Columns.CONFIG_ID, Columns.TYPE, Columns.NAME, Columns.CREATE_TIME, Columns.UPDATE_TIME, Columns.STATUS, Columns.APP_ID, Columns.ENV_ID, Columns.VERSION });
        //        }
        try {
            DaoPageResult<Config> pageresult = new DaoPageResult<Config>();
            StringBuffer sb = new StringBuffer(100);
            sb.append("  appid = ");
            sb.append(appid);
            sb.append(" and envid=");
            sb.append(envid);
            sb.append(" and version= '");
            sb.append(version);
            sb.append("' and status =  ");
            sb.append(Constants.STATUS_NORMAL);
            List<Config> configlist = DBHelper.getInstance().getDao().sql.getListByCustom(Config.class, "", sb.toString(), "");
            return configlist;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public void deleteItem(Long configId) {
        //        String curTime = DateUtils.format(new Date(), DataFormatConstants.COMMON_TIME_FORMAT);
        //        List<Modify> modifyList = new ArrayList<Modify>();
        //        modifyList.add(modify(Columns.STATUS, Constants.STATUS_DELETE));
        //        modifyList.add(modify(Columns.UPDATE_TIME, curTime));
        //
        //        update(modifyList, match(Columns.CONFIG_ID, configId));
        try {
            DBHelper.getInstance().getDao().sql.upateByID(Config.class, " status = " + Constants.STATUS_DELETE, configId);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void updateValue(Long configId, String value) {

        // 时间
        //        String curTime = DateUtils.format(new Date(), DataFormatConstants.COMMON_TIME_FORMAT);
        //
        //        List<Modify> modifyList = new ArrayList<Modify>();
        //        modifyList.add(modify(Columns.VALUE, value));
        //        modifyList.add(modify(Columns.UPDATE_TIME, curTime));
        //
        //        update(modifyList, match(Columns.CONFIG_ID, configId));
        try {
            DBHelper.getInstance().getDao().sql.upateByID(Config.class, " value = " + value, configId);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String getValue(Long configId) {
        try {
            Config config = (Config) DBHelper.getInstance().getDao().sql.get(Config.class, configId);
            return config.getValue();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
